﻿//---------------------------------------------------------------------
//Copyright (c) 2009, Pierre Milet
//All rights reserved.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
//OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
//PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
//---------------------------------------------------------------------
using System;
using System.Xml;
using System.Threading;

using Microsoft.BizTalk.Component.Interop;
using Microsoft.BizTalk.TransportProxy.Interop;
using Microsoft.BizTalk.Message.Interop;

using Microsoft.Samples.BizTalk.Adapter.Common;

namespace BizMock.Adapter.Runtime
{
    /// <summary>
    /// This is a singleton class for the BizMock send adapter. All the messages, going to various
    /// send ports of this adapter type, will go through this class.
    /// 
    /// Messages will be delivered to this adapter in batches. The batch implementation is provided
    /// by the BizMockTransmitAdapterBatch class.
    /// </summary>
    sealed public class BizMockTransmiter : AsyncTransmitter
    {
        private static string BizMockNamespace = "http://schemas.bizmock/BizMockAdapter";

        public BizMockTransmiter() : base(
                "BizMock Transmit Adapter",
                "1.0",
                "sends messages to the BizMock test environment and receives responses",
                "BizMock",
                new Guid("fa0588ed-3353-4b70-9d04-f001c23b9f17"),
                BizMockNamespace, 
                typeof(BizMockTransmitterEndpoint), 
				10)
        {
        }

		protected override IBTTransmitterBatch CreateAsyncTransmitterBatch()
		{
			return new BizMockTransmiterBatch(this.MaxBatchSize, BizMockNamespace, TransportProxy, this);		
		}

        public ConfigProperties CreateProperties(string uri)
        {
            ConfigProperties properties = new BizMockTransmitProperties(uri);
            return properties;
        }

        protected override void HandlerPropertyBagLoaded()
        {
            IPropertyBag config = this.HandlerPropertyBag;
            if (null != config)
            {
                XmlDocument handlerConfigDom = ConfigProperties.IfExistsExtractConfigDom(config);
                if (null != handlerConfigDom)
                {
                    BizMockTransmitProperties.TransmitHandlerConfiguration(handlerConfigDom);
                }
            }
        }
    }
}
